<template>
	<a-card :bordered="false">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<!-- eslint-disable-next-line vue/no-useless-template-attributes -->
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizGroupAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('bizGroupBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchBizGroup"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a-space>
							<a-button type="link" size="small" @click="userListRef.showModal(record, record.id)">查看成员</a-button>
						</a-space>
						<a-divider type="vertical" v-if="hasPerm(['bizGroupEdit', 'bizGroupDelete'], 'and')" />
						<a-popconfirm title="确定加入该社团？" @confirm="joinGroup(record)">
							<a-button type="link" size="small">申请加入</a-button>
						</a-popconfirm>
						<a-divider type="vertical" v-if="hasPerm(['bizGroupEdit', 'bizGroupDelete'], 'and')" />
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizGroupEdit')">编辑</a>

						<a-divider type="vertical" v-if="hasPerm(['bizGroupEdit', 'bizGroupDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizGroup(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizGroupDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<UserList ref="userListRef" />
</template>

<script setup name="bizGroup">
	import Form from './form.vue'
	import UserList from './userList/index.vue'
	import bizGroupApi from '@/api/biz/bizGroupApi'
	import { globalStore } from '@/store'
	import { message } from 'ant-design-vue'
	const store = globalStore()
	const tableRef = ref()
	const formRef = ref()
	const userListRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '社团名称',
			dataIndex: 'groupName',
			align: 'center'
		},
		{
			title: '社团标签',
			dataIndex: 'groupTag',
			align: 'center'
		},
		{
			title: '管理员/创建人',
			dataIndex: 'managerName',
			align: 'center'
		},
		{
			title: '社团分类',
			dataIndex: 'groupCate',
			align: 'center'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizGroupEdit', 'bizGroupDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 400
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		return bizGroupApi.bizGroupPage(parameter).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizGroup = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizGroupApi.bizGroupDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizGroup = (params) => {
		bizGroupApi.bizGroupDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}

	// 申请加入社团
	const joinGroup = (record) => {
		const userInfo = store.userInfo
		let params = {
			userId: userInfo.valueOf().id,
			groupId: record.id
		}
		bizGroupApi.bizGroupJoin(params).then(() => {
			tableRef.value.refresh(true)
			message.success('申请成功！请等待管理员通过')
		})
	}
	// 查看社团成员
	const checkMember = (record) => {}
</script>
